﻿#include "MirInputBox.h"
#include "../MirGraphics/MLibrary.h"
#include "../Settings.h"
#include "MirScene.h"
#include "../Program.h"

using namespace Client::MirGraphics;

namespace Client::MirControls
{

    MirInputBox::MirInputBox(const std::string &message) : CaptionLabel(new MirLabel();
        CaptionLabel->setDrawFormat(TextFormatFlags::WordBreak);
        Point tempVar(25, 25);
        CaptionLabel->setLocation(&tempVar);
        Size tempVar2(235, 40);
        CaptionLabel->setSize(&tempVar2);
        CaptionLabel->setParent(this);
        CaptionLabel->setText(message)), OKButton(new MirButton();
        OKButton->setHoverIndex(201);
        OKButton->setIndex(200);
        OKButton->setLibrary(Libraries::Title);
        Point tempVar3(60, 123);
        OKButton->setLocation(&tempVar3);
        OKButton->setParent(this);
        OKButton->setPressedIndex(202)), CancelButton(new MirButton();
        CancelButton->setHoverIndex(204);
        CancelButton->setIndex(203);
        CancelButton->setLibrary(Libraries::Title);
        Point tempVar4(160, 123);
        CancelButton->setLocation(&tempVar4);
        CancelButton->setParent(this);
        CancelButton->setPressedIndex(205)), InputTextBox(new MirTextBox();
        InputTextBox->setParent(this);
        InputTextBox->setBorder(true);
        InputTextBox->setBorderColour(Color::Lime);
        Point tempVar5(23, 86);
        InputTextBox->setLocation(&tempVar5);
        Size tempVar6(240, 19);
        InputTextBox->setSize(&tempVar6);
        InputTextBox->setMaxLength(50))
        {
        setModal(true);
        setMovable(false);

        setIndex(660);
        setLibrary(Libraries::Prguse);

        Point tempVar7((Settings::ScreenWidth - getSize()->Width) / 2, (Settings::ScreenHeight - getSize()->Height) / 2);
        setLocation(&tempVar7);


        InputTextBox->SetFocus();
        InputTextBox->TextBox->KeyPress += MirInputBox_KeyPress;


        CancelButton->Click += DisposeDialog;
        }

    void MirInputBox::MirInputBox_KeyPress(std::any sender, KeyPressEventArgs *e)
    {
        if (e->KeyChar == static_cast<char>(Keys->Enter))
        {
            if (OKButton != nullptr && !OKButton->isDisposed())
            {
                OKButton->InvokeMouseClick(EventArgs::Empty);
            }
            e->Handled = true;
        }
        else if (e->KeyChar == static_cast<char>(Keys->Escape))
        {
            if (CancelButton != nullptr && !CancelButton->isDisposed())
            {
                CancelButton->InvokeMouseClick(EventArgs::Empty);
            }
            e->Handled = true;
        }
    }

    void MirInputBox::DisposeDialog(std::any sender, EventArgs *e)
    {
        Dispose();
    }

    void MirInputBox::OnKeyDown(KeyEventArgs *e)
    {
        MirImageControl::OnKeyDown(e);
        e->Handled = true;
    }

    void MirInputBox::OnKeyUp(KeyEventArgs *e)
    {
        MirImageControl::OnKeyUp(e);
        e->Handled = true;
    }

    void MirInputBox::OnKeyPress(KeyPressEventArgs *e)
    {
        MirImageControl::OnKeyPress(e);

        if (e->KeyChar == static_cast<char>(Keys->Escape))
        {
            if (CancelButton != nullptr && !CancelButton->isDisposed())
            {
                CancelButton->InvokeMouseClick(EventArgs::Empty);
            }
        }
        else if (e->KeyChar == static_cast<char>(Keys->Enter))
        {
            if (OKButton != nullptr && !OKButton->isDisposed())
            {
                OKButton->InvokeMouseClick(EventArgs::Empty);
            }

        }
        e->Handled = true;
    }

    void MirInputBox::Show()
    {
        if (getParent() != nullptr)
        {
            return;
        }

        setParent(MirScene::ActiveScene);

        Highlight();

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = dynamic_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }
    }

    void MirInputBox::Dispose(bool disposing)
    {
        MirImageControl::Dispose(disposing);

        if (!disposing)
        {
            return;
        }

        for (int i = 0; i < Program::Form->Controls->Count; i++)
        {
            TextBox *T = static_cast<TextBox*>(Program::Form->Controls[i]);
            if (T != nullptr && T->Tag != nullptr && T->Tag != nullptr)
            {
                (static_cast<MirTextBox*>(T->Tag))->DialogChanged();
            }
        }
    }
}
